package org.thss.petri.gef.editparts;

import org.eclipse.draw2d.ChopboxAnchor;
import org.eclipse.draw2d.ConnectionAnchor;
import org.eclipse.draw2d.IFigure;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.util.EList;
import org.eclipse.gef.ConnectionEditPart;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.NodeEditPart;
import org.eclipse.gef.Request;
import org.thss.petri.gef.editparts.common.PetriNetNodeEditPart;
import org.thss.petri.gef.editparts.policies.TransitionFireEditPolicy;
import org.thss.petri.gef.figures.NodeFigure;
import org.thss.petri.gef.figures.TransitionFigure;
import org.thss.petri.model.PetriPackage;
import org.thss.petri.model.Transition;


public class TransitionEditPart extends PetriNetNodeEditPart implements NodeEditPart {
	
	public TransitionEditPart(Transition model) {
		super(model);
	}

	@Override
	protected IFigure createFigure() {
		TransitionFigure figure = new TransitionFigure();
		return figure;
	}
	
	@Override
	protected void refreshVisuals() {
		TransitionFigure figure = (TransitionFigure) getFigure();
		boolean enabled = ((Transition) getModel()).isEnabled();
		figure.setEnabled(enabled);
		super.refreshVisuals();
	}

	@Override
	protected ConnectionAnchor getNodeAnchor() {
		NodeFigure figure = (NodeFigure) getFigure();
		return new ChopboxAnchor(figure.getNodeShape());
	}

	@Override
	public EList getModelSourceConnections() {
		return ((Transition) model).getOutgoings();
	}

	@Override
	public EList getModelTargetConnections() {
		return ((Transition) model).getIncomings();
	}

	@Override
	protected void createEditPolicies() {
		super.createEditPolicies();
		installEditPolicy(EditPolicy.NODE_ROLE,new TransitionFireEditPolicy()); 
	}

	@Override
	public void notifyChanged(Notification notification) {
		int id = notification.getFeatureID(PetriPackage.class);
		switch (id) {
		case PetriPackage.TRANSITION__INCOMINGS:
		case PetriPackage.TRANSITION__ENABLED:
			refreshVisuals();
			break;
		}
		super.notifyChanged(notification);
	}
	
}
